<{capture name="header"}>
<{css app="ome" src="ome.css"}>
<{css app="ome" src="style.css"}>
<{script src="coms/modedialog.js" app="desktop"}>
<{script src="coms/pager.js" app="desktop"}>
<{/capture}>
<style>
    .notice-inline.error  {background-image:none;margin-left:25px}
    #fm1 .division td label ,#fm1 .division div label {vertical-align:top;}
</style>
<form method="post" id="fm1" action="index.php?app=material&ctl=admin_material_basic&act=toAdd">
    <div class="tableform">
        <h3>新建基础物料</h3>
        <div class="division">
            <h5>基本信息</h5>
            <table border="0" cellspacing="0" cellpadding="0" class="gridlist">
                <thead>
                <tr>
                    <td width="10%" align="right" nowrap="nowrap">
                        物料名称：
                    </td>
                    <td>
                        <input type="text" name="material_name" size="60" vtype="required"/>
                        <span style="color:red;">*</span>
                    </td>
                    <td width="10%" align="right" nowrap="nowrap">物料类型：</td>
                    <td>
                        <select name="goods_type">
                            <option value="0">--请选择物料类型--</option>
                            <{foreach from=$goods_types item=types}>
                                <option value="<{$types.type_id}>"><{$types.name}></option>
                            <{/foreach}>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td width="10%" align="right" nowrap="nowrap">
                        物料编号：
                    </td>
                    <td>
                        <input type="text" name="material_bn" vtype="required" onkeyup="value=value.replace(/[^A-Za-z0-9_-]/ig,'')"/>
                        <span style="color:red;">*</span>
                    </td>

                    <td width="10%" align="right" nowrap="nowrap">物料规格：</td>
                    <td>
                        <input type="text" name="material_specification" maxlength="20" />
                        <{help}><{t}>规格将在备货单和发货单中展示<{/t}><{/help}>
                    </td>
                </tr>
                <tr>
                    <td width="10%" align="right" nowrap="nowrap">
                        物料条码：
                    </td>
                    <td>
                        <input type="text" name="material_code" vtype="required" onkeyup="value=value.replace(/[^A-Za-z0-9_-]/ig,'')"/>
                        <span style="color:red;">*</span>
                    </td>
                    <td width="10%" align="right" nowrap="nowrap">物料品牌：</td>
                    <td>
                        <select id="brand" name="brand">
                            <option value="0">--请选择品牌--</option>
                            <{foreach from=$material_brand item=brand}>
                            <option value="<{$brand.brand_id}>"><{$brand.brand_name}></option>
                            <{/foreach}>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td width="10%" align="right" nowrap="nowrap">
                        是否启用唯一码：
                    </td>
                    <td>
                        <input type="radio" value="true" name="serial_number" >是
                        <input type="radio" value="false" name="serial_number" checked="checked">否
                    </td>

                    <td width="10%" align="right" nowrap="nowrap">特殊扫码配置：</td>
                    <td>
                        <input type="radio" name="special_setting" id="openconf" value="3"/>配置
                        <input type="radio" name="special_setting" checked value="4" />关闭
                    </td>

                </tr>
                <tr>
                    <td width="10%" align="right">是否在售：</td>
                    <td>
                        <input type="radio" name="visibled" value="1" checked="checked"/> 在售 <input type="radio" name="visibled" value="2" /> 停售
                        <!--<span style="color:red;">(不在销售的物料，不可做查询或参与实际的订单销售等业务)</span>-->
                    </td>
                    <td colspan="2" rowspan="2" style="width: 550px; padding-left: 5%">
                        <div id="description" style="border: 0px solid black;width: 500px;padding: 10px 50px;letter-spacing: 1px;display: none;background-color: #FDFDDB;">
                            扫码中识别整个条码中第
                            <input type="text" name="first_num" vtype="required" value="1"/><span style="color:red;">*</span>
                            位到第
                            <input type="text" name="last_num" vtype="required"  value=""/><span style="color:red;">*</span>
                            <br />位为扫码出库的条码
                            即扫码作业中根据配置的第X到Y位的条码与物料条码一致<br />
                            则视作该商品进行出库。
                        </div>
                    </td>
                </tr>
                <tr>
                    <td width="10%" align="right">物料属性：</td>
                    <td>
                        <input type="radio" name="type" value="1" checked="checked"/> 普通 <span style='display: none'><input type="radio" name="type" value="2" /> 组合</span>
                    </td>
                </tr>
                <tr>
                    <td width="10%" align="right">是否全渠道：</td>
                    <td>
                        <input type="radio" name="omnichannel" value="1" /> 开启 <input type="radio" name="omnichannel" value="2" checked="checked" /> 关闭
                    </td>
                </tr>
                </thead>
            </table>

            <div id="combination_items" style="margin-top:10px;display:none">
                <h5>组合物料明细</h5>
                <div class="gridlist" style="padding:10px;">

                    <{button label="物料库频道" id="material-find-btn" }>

                    <table class="gridlist" id="material_table"  style="margin:4px 0;">
                        <thead>
                        <tr>
                            <th>编码</th>
                            <th style="width:240px;">名称</th>
                            <th>数量</th>
                            <th style="width:30px;">删除</th>
                        </tr>
                        </thead>
                        <tbody id="dataNode">
                        <tr>
                            <td colspan="7" style="padding:0;"><div class="note" style="margin:0;"> 暂无物料信息 </div></td>
                        </tr>
                        </tbody>
                    </table>
                    <div align="right">
                        <{button type="button" id="material-delall-btn" label="全部删除" }>
                    </div>
                </div>
            </div>

            <{if $show_storage_life}>
            <div id="warn_div">
                <h5 style="margin-top:10px;">保质期管理</h5>
                <table border="0" cellspacing="0" cellpadding="0" class="gridlist">
                    <thead>
                    <tr>
                        <td width="10%" align="right" nowrap="nowrap">
                            是否启用保质期监控：
                        </td>
                        <td>
                            <input type="radio" name="use_expire" value="1" /> 开启 <input type="radio" name="use_expire" value="2" checked="checked"/> 关闭
                        </td>
                    </tr>
                    <tr class="use_expire_conf" style="display:none;">
                        <td width="10%" align="right" nowrap="nowrap">
                            预警天数配置：
                        </td>
                        <td>
                            <input type="text" name="warn_day" vtype="number&required"/>
                        </td>
                    </tr>
                    <tr class="use_expire_conf" style="display:none;">
                        <td width="10%" align="right" nowrap="nowrap">
                            自动退出库存天数配置：
                        </td>
                        <td>
                            <input type="text" name="quit_day" vtype="number&required" />
                        </td>
                    </tr>
                    </thead>
                </table>
            </div>
            <{/if}>

            <h5 style="margin-top:10px;">扩展信息</h5>
            <table border="0" cellspacing="0" cellpadding="0" class="gridlist">
                <thead>
                <tr>
                    <td width="10%" align="right" nowrap="nowrap">
                        成本价：
                    </td>
                    <td>
                        <input type="text" name="cost" value="" onkeyup="value=value.replace(/[^\d\.]/ig,'')"/>
                    </td>
                </tr>
                <tr>
                    <td width="10%" align="right" nowrap="nowrap">
                        售价：
                    </td>
                    <td>
                        <input type="text" name="retail_price" value="" onkeyup="value=value.replace(/[^\d\.]/ig,'')"/>
                    </td>
                </tr>
                <tr>
                    <td width="10%" align="right" nowrap="nowrap">
                        重量：
                    </td>
                    <td>
                        <input type="text" name="weight" value="" />&nbsp;&nbsp;<span style="color:red;">重量单位:克(g)</span>
                    </td>
                </tr>
                <tr>
                    <td width="10%" align="right" nowrap="nowrap">
                        包装单位：
                    </td>
                    <td>
                        <input type="text" name="unit" value="" />
                    </td>
                </tr>
                </thead>
            </table>
        </div>

        <div id="cc" class="noprint table-action">
            <{button type="button" id="material-save-btn" label="保 存"}> &nbsp;
            <{button type="button" class="btn-secondary" id="return-btn" label="关 闭" onclick="javascript:void(window.close());"}>
        </div>
    </div>
</form>
<script>
    (function() {
        window.addEvent('domready', function() {
            $ES('input[name=type]').each(function(item){
                item.addEvent('click',function(e){
                    if(this.value == '1'){
                        $('combination_items').setStyle('display','none');
                    }else{
                        $('combination_items').setStyle('display','block');
                    }
                })
            });

            $ES('input[name=special_setting]').each(function(item){
                item.addEvent('click', function(e){
                    if(this.value == '3'){
                        $('description').setStyle('display','block');
                        $('warn_div').setStyle('display','none');
                    }else{
                        $('description').setStyle('display','none');
                        $('warn_div').setStyle('display','block');
                    }
                })
            });

            $ES('input[name=use_expire]').each(function(item){
                item.addEvent('click',function(e){
                    if(this.value == '1'){
                        $ES('.use_expire_conf').setStyle('display','table-row');
                        $ES('#openconf').set('disabled', true);
                    }else{
                        $ES('.use_expire_conf').setStyle('display','none');
                        $ES('#openconf').set('disabled', false);
                    }
                })
            });

            var callurl='index.php?app=material&ctl=admin_material_basic&act=getMaterial',store=[];

            var tpl='<tr key="{bm_id}" id="bm_{bm_id}" title="点击选取/反选此条记录">'
                    +'  <td>{material_bn}</td><td class="material-name">{material_name}</td>'
                    +'  <td><input type="text" value="{material_num}" key="num" vtype="number&amp;&amp;required" tname="at[_PRIMARY_]" size="6"></td>'
                    +'  <td><{img src="bundle/delecate.gif" app="desktop" key="state" class="pointer btn-delete-item"}></td>'
                    +'</tr>';

            $('material-find-btn').addEvent('click',function(e){
                var url='index.php?app=desktop&act=alertpages&goto='+encodeURIComponent('index.php?app=material&ctl=admin_material_basic&act=findMaterial&type=1');
                new finderDialog(url,{params:{url:callurl,name:'bm_id[]'},width:1000,height:660,
                    onCallback:function(rs){
                        if(!rs)return;
                        rs=JSON.decode(rs);
                        init(rs);
                    }
                });
            });

            var pag,rows;
            function emptyData(){
                var noData='<tr>'
                        +'<td colspan="7" style="padding:0;"><div class="note" style="margin:0;"> 暂无物料信息 </div></td>'
                        +'</tr>';
                $('dataNode').set('html',noData);
            }

            function createProduct(data){
                pag=new PageData(tpl,data,{'updateMain':$('dataNode'),'pageNum':5,PRIMARY_ID:'bm_id',
                    'onShow':function(){
                        var _this=this;
                        $$('#dataNode input[type]').addEvent('change',function(e){
                            var pid=this.getParent('tr').get('key'),value=this.value;

                            _this.editData(pid,[this.get('key'),value]);
                        });

                        rows=$ES('#dataNode tr');
                        rows.each(function(item,i){
                            item.addEvent('click',function(e){
                                this.toggleClass('selected');
                            });
                            item.getElement('.btn-delete-item').addEvent('click',function(e){
                                if(_this.selectData(item.get('key')) && confirm('确定要删除 '+ _this.selectData(item.get('key'))['material_name'] +' 吗？')) _this.delData(item.get('key'));
                                if(!$E('#dataNode tr')) emptyData();
                            });
                            item.getElement('input[tname^=at]').addEvent('keypress',function(e){
                                if(e.code==13) $E('#pfba input').focus();
                            });
                        });

                        if(this.data.length) rows[0].getElement('input[key^=num]').focus();

                        $ES('.material-name').removeEvent('mouseover').addEvent('mouseover',function(e){
                            if (this.get('visibility')=='false')
                            {
                                var e  = new Event(e), el = e.target;
                                visiTips.attach(el);
                                el.addEvent('mouseleave',function(){
                                    this.removeClass('active');
                                });
                                el.fireEvent('mouseenter',e);
                            }
                        });
                    }
                });
            }

            function init(rs){
                var tmparr=findProduct(rs,'bm_id');
                store.unshift.apply(store,tmparr.reverse());
                createProduct(store);
            }

            function findProduct(arr,PRIMARY){
                if(!store.length)return arr;
                store.each(function(a){
                    arr.each(function(b){
                        if(a[PRIMARY]==b[PRIMARY])arr.erase(b);
                    });
                });
                return arr;
            }

            function delProduct(obj,arr){
                arr.each(function(d){obj.delData(d);});
            }

            $('material-delall-btn').addEvent('click',function(e){
                if(!pag||!pag.data)return;
                var delarr=[];
                pag.data.each(function(d){
                    delarr.push(d['bm_id']);
                });
                if(confirm('确认删除全部物料吗？')){
                    delProduct(pag,delarr);
                    emptyData();
                }
            });

            $('material-save-btn').addEvent('click',function(e){
                var _this=this;
                var form=this.getParent('form');

                var use_expire  = $ES("input[name='use_expire']:checked").get('value');
                if(use_expire == '1')
                {
                    var warn_day    = $ES("input[name='warn_day']").get('value');
                    var quit_day    = $ES("input[name='quit_day']").get('value');

                    warn_day    = parseInt(warn_day);
                    quit_day    = parseInt(quit_day);

                    if(isNaN(warn_day) || warn_day <=0 || isNaN(quit_day) || quit_day <=0)
                    {
                        alert('请填写保质期监控配置');
                        return false;
                    }

                    if(warn_day <= quit_day)
                    {
                        alert('预警天数必须大于自动退出库存天数');
                        return false;
                    }
                }

                if(pag){
                    var data=pag.toHideInput($('dataNode').getElement('tr'));
                    form.store('target',{extraData:data,
                        onRequest:function(){
                            _this.disabled=true;
                        },
                        onComplete:function(jsontext){
                            try{
                                var json = JSON.decode(jsontext);
                                if (typeof(json.error)!='undefined'){
                                    _this.disabled=false;
                                }else{
                                    _this.disabled=true;
                                    if(opener.finderGroup['<{$env.get.finder_id}>']) opener.finderGroup['<{$env.get.finder_id}>'].refresh.delay(100,opener.finderGroup['<{$env.get.finder_id}>']);
                                    setTimeout('window.close()',200);
                                }
                            }catch(e){}
                        }
                    });
                }else{
                    form.store('target',{
                        onRequest:function(){
                            _this.disabled=true;
                        },
                        onComplete:function(jsontext){
                            try{
                                var json = JSON.decode(jsontext);
                                if (typeof(json.error)!='undefined'){
                                    _this.disabled=false;
                                }else{
                                    _this.disabled=true;
                                    if(opener.finderGroup['<{$env.get.finder_id}>']) opener.finderGroup['<{$env.get.finder_id}>'].refresh.delay(100,opener.finderGroup['<{$env.get.finder_id}>']);
                                    setTimeout('window.close()',200);
                                }
                            }catch(e){}
                        }
                    });
                }
                form.fireEvent('submit',e);
            });
        });
    })();

    function feature_group_object_callback(rs,handle){
        var p_node = handle.getParent('div');
        var html = '已选择了1个类目,'+"<a href='javascript:void(0);' onclick='feature_group_selected_show()'>查看选中的类目.</a>";
        if ($defined($('hand-selected-product')))
        {
            $('hand-selected-product').setHTML(html);
        } else {
            var div = new Element('div',{'html':html,'id':'hand-selected-product'});
            div.injectAfter(p_node);
        }
    }

    function feature_group_selected_show(){
        new Dialog('index.php?app=material&ctl=admin_material_basic&act=showFeatureGrp',{
            ajaxoptions:{data:$('hand-selected-product').getPrevious('div'),method:'post'}
        });
    }
</script>